import pygame
from pygame.locals import *
import math
GRAVATATIONAL_CONSTANT = .03
pygame.init()

screen = pygame.display.set_mode((640, 480), 0, 32)

class Planetoid():
	def __init__(self, position, size, velocity=[0,0], name=None):
		self.x, self.y = position
		self.velocity = velocity
		self.direction = 0
		self.mass = math.pi * (size ** 2)
		self.size = size
		self.speed = 1
		self.forcesList = []
		self.rect = Rect(self.x-self.size, self.y - self.size, self.size*2, self.size*2)
		self.name = name

	def update(self):
		self.rect = Rect(self.x-self.size, self.y - self.size, self.size*2, self.size*2)
		self.forcesList = []
		self.x += self.velocity[0] * self.speed
		self.y += self.velocity[1] * self.speed
		pygame.draw.circle(screen, (255,0,0), (int(self.x), int(self.y)), self.size)

	def calculate_force(self, otherMass):
		distance = math.sqrt(abs(self.x - otherMass.x)**2 + 
						 (abs(self.y) - otherMass.y)**2 )
		direction = ( (self.x - otherMass.x) / distance,
					  (self.y - otherMass.y) / distance )
		force = (self.mass * otherMass.mass) / distance ** 2
		return direction, force * GRAVATATIONAL_CONSTANT, distance

def place_new_planet():
	mousePos = pygame.mouse.get_pos()
	global planetPos
	global planetSize
	global timesMouseClicked
	if timesMouseClicked is 1:
		if pygame.mouse.get_pressed()[0] == 1:
			pygame.draw.circle(screen, (255,0,255), mousePos, 30)
		if pygame.event.get(MOUSEBUTTONUP) and mouseButtonPressed: 
			#"and mouseButtonPressed" prevents scrolling from triggering this code block 
			planetPos = mousePos
			planetSize = 30
			timesMouseClicked += 1
	if timesMouseClicked is 2:
		pygame.draw.circle(screen, (255,0,255), planetPos, 30)
		pygame.draw.line(screen, (255, 0, 0), planetPos, mousePos)
		if pygame.event.get(MOUSEBUTTONUP) and mouseButtonPressed:
			distance = math.sqrt(abs(mousePos[0] - planetPos[0])**2 + 
							 (abs(mousePos[1]) - planetPos[1])**2 )
			direction = ( (mousePos[0] - planetPos[0]) / distance,
						  (mousePos[1] - planetPos[1]) / distance )
			velocity = [ (distance * .001) * direction[0],
						 (distance * .001) * direction[1] ]
			planets.append(Planetoid(planetPos, planetSize, velocity))
			timesMouseClicked = 1
			planetPos = None
	
#some stuff to do with adding new planet:
planetPos = None
planetSize = None
timesMouseClicked = 1
		
clock = pygame.time.Clock()
planets = [Planetoid((320, 240), 50, [0,0]), Planetoid((310, 340), 10, [-1.3, -.7])]
planets[1].mass = .0000000000000000000001
statusText = pygame.font.Font(None, 20)
while True:
	clock.tick(60)
	screen.fill((255,255,255))
	pygame.event.pump()
	place_new_planet()
	for planet in planets:
		if (planet.x < -100 or planet.x > 740 or
		   planet.y < -100 or planet.y > 600):
		   planets.remove(planet)
		   continue
		for otherPlanets in planets:
			if otherPlanets is planet:
				continue
			otherPlanets.forcesList.append(otherPlanets.calculate_force(planet))
	for planet in planets:
		averageForce = [0,0]
		for force in planet.forcesList:
			averageForce[0] += force[1] *  force[0][0]
			averageForce[1] += force[1] *  force[0][1]
		distance = math.sqrt(abs(averageForce[0])**2 + 
						 (abs(averageForce[1]))**2 )
		if distance == 0: distance = .0001
		direction = (averageForce[0] / distance, averageForce[1] / distance)
		planet.velocity[0] += abs((averageForce[0])/(planet.mass)) * -direction[0]
		planet.velocity[1] += abs((averageForce[1])/(planet.mass)) * -direction[1]			
		planet.update()
	mouseButtonPressed = pygame.mouse.get_pressed()[0]
	pygame.event.clear()
	pygame.display.update()